package com.sgb.trip.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.sgb.link.router.PathTrip;
import com.sgb.link.view.fragment.AppLazyFragment;
import com.sgb.trip.R;
import com.sgb.trip.databinding.TripFragmentFlightOrdersBinding;
import com.sgb.trip.entity.FlightOrdersInfoEntity;
import com.sgb.trip.view.activity.FlightOrdersDetailSelfActivity;
import com.sgb.trip.view.adapter.FlightOrdersAdapter;
import com.sgb.trip.viewmodel.FlightOrdersSelfModel;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.view.ChooseTwoTimePopWindow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/9/8
 * Author:黄淋
 * Description:机票订单
 */
public class FlightOrdersSelfFragment extends AppLazyFragment {
    TripFragmentFlightOrdersBinding mBinding;
    FlightOrdersAdapter mAdapter;
    private ChooseTwoTimePopWindow mTimePop;//选择时间

    FlightOrdersSelfModel mViewModel;

    private int position;
    boolean isChange = false;

    Map<String, Object> map;
    private boolean mIsLoadMore = false;

    public static FlightOrdersSelfFragment newInstance(int position) {
        FlightOrdersSelfFragment fragment = new FlightOrdersSelfFragment();
        Bundle args = new Bundle();
        args.putInt(RouterKey.INT, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.trip_fragment_flight_orders, container, false);
        position = getArguments().getInt(RouterKey.INT);
        mViewModel = new ViewModelProvider(this).get(FlightOrdersSelfModel.class);
        initAdapter();
        initWindow();
        mBinding.tvFragmentFlightOrderTime.setOnClickListener(this);

        map = new HashMap<>();
        return mBinding.getRoot();
    }

    private void initAdapter() {
        mBinding.rvFlightOrders.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new FlightOrdersAdapter(getContext(), null);
        mAdapter.setOnItemClick(new AdapterOnItemClick<FlightOrdersInfoEntity>() {
            @Override
            public void onItemClick(FlightOrdersInfoEntity flightOrdersInfoEntity, int position) {
                ARouter.getInstance().build(PathTrip.FlightOrdersDetailSelfActivity).withString(RouterKey.STRING, flightOrdersInfoEntity.getOrderNo()).navigation();
            }
        });
        mAdapter.setOnClickItem(new FlightOrdersAdapter.OnClickItem() {
            @Override
            public void onApplyPay(FlightOrdersInfoEntity entity) {
                MToast.showToast(getContext(), "功能开发中");
            }
        });

        mBinding.rvFlightOrders.setAdapter(mAdapter);
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mIsLoadMore = true;
                getData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mIsLoadMore = false;
                getData();
            }
        });

    }

    private void getData() {
        DialogHelper.showProgressDialog(getContext(), "加载中...").setCanceledOnTouchOutside(false);
        mViewModel.getFlightOrdersList(mIsLoadMore, map);
    }

    /**
     * 初始化选择时间弹窗
     */
    private void initWindow() {
        mTimePop = new ChooseTwoTimePopWindow(getContext());
        mTimePop.setBgView(mBinding.grayLayoutFlight);

        mTimePop.setOnResultClick((key1, key2, key3) -> {
            long startTime = (long) key1;
            long endTime = (long) key2;
            isChange = startTime != 0 && endTime != 0;
            mBinding.tvFragmentFlightOrderTime.contentSelect(true, false);
            if (startTime == 0 && endTime != 0) {
                MToast.showToast(getContext(), "请选择开始时间");
                return;
            }
            if (endTime == 0 && startTime != 0) {
                MToast.showToast(getContext(), "请选择结束时间");
                return;
            }
            map.put("createTimeForStart", DateUtils.stampToDay6(startTime));
            map.put("createTimeForEnd", DateUtils.stampToDay6(endTime));
            mIsLoadMore = false;
            getData();
        });
        mTimePop.setOnDisMissClick(new BasePopWindow.OnDisMiss() {
            @Override
            public void disMiss() {
                if (isChange) {
                    mBinding.tvFragmentFlightOrderTime.contentSelect(true, false);
                } else {
                    mBinding.tvFragmentFlightOrderTime.contentSelect(false, false);
                }
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        switch (position) {
            case 0://全部

                break;
            case 1://待支付
                map.put("orderTags", 0);
                break;
            case 2://已关闭
                map.put("orderTags", 4);
                break;

        }
        mIsLoadMore = false;
        getData();
        mViewModel.ldOrdersData.observe(this, list -> showProjectList(list, mIsLoadMore));
    }

    /**
     * 数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showProjectList(List<FlightOrdersInfoEntity> list, boolean isLoadMore) {
        DialogHelper.dismissProgressDialog();
        mBinding.refreshLayout.finishRefresh();
        mBinding.refreshLayout.finishLoadMore();
        mAdapter.setCancelTimer();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                mBinding.refreshLayout.resetNoMoreData();
                mAdapter.addDatas(list);
            } else {
                mBinding.refreshLayout.finishLoadMoreWithNoMoreData();
            }
            mBinding.rvFlightOrders.setVisibility(View.VISIBLE);
        } else {
            if (null != list && list.size() > 0) {
                mBinding.rvFlightOrders.setVisibility(View.VISIBLE);
                mBinding.errorView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
            } else {
                mAdapter.setDatas(null);
                mBinding.rvFlightOrders.setVisibility(View.GONE);
                mBinding.errorView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_fragment_flight_order_time) {
            mBinding.tvFragmentFlightOrderTime.contentSelect(false, true);
            mTimePop.showPopwindow0(mBinding.llFilter);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mAdapter.setCancelTimer();
    }
}
